/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.gwt.baselib.ui.client.widgets;

import nl.biz_e.gwt.baselib.ui.client.util.UiUtils;

import com.google.gwt.dom.client.Element;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Image;

/**
 * A widget that represents a simple &lt;a&gt; element.<br/>
 * This is an extension to the default {@link Anchor} widget provided by
 * the GWT. Unlike the default {@link Anchor}, this widget also accepts
 * {@link Image}s and {@link ImageResource}s as content for the anchor.
 * 
 * <h3>CSS Style Rules</h3> <li>.gwt-Anchor { }</li> </ul>
 * 
 * @see Anchor
 */
public class ImageAnchor extends Anchor {
	
	/**
	 * Constructs a new, empty Anchor
	 * 
	 * @see Anchor
	 */
	public ImageAnchor() {
		super();
	}
	
	/**
	 * This constructor may be used by subclasses to explicitly use an existing
	 * element. This element must be an &lt;a&gt; element.
	 * 
	 * @param element
	 *        the element to be used
	 * @see Anchor
	 */
	protected ImageAnchor(Element element) {
		super(element);
	}
	
	/**
	 * Creates a source anchor (link to URI).
	 * 
	 * That is, an anchor with an href attribute specifying the destination URI.
	 * 
	 * @param text
	 *        the anchor's text
	 * @param asHtml
	 *        asHTML <code>true</code> to treat the specified text as
	 *        html
	 * @param href
	 *        the url to which it will link
	 * @param target
	 *        the target frame (e.g. "_blank" to open the link in a new
	 *        window)
	 * @see Anchor
	 */
	public ImageAnchor(String text, boolean asHtml, String href, String target) {
		super(text, asHtml, href, target);
	}
	
	/**
	 * Creates an anchor with its text and href (target URL) specified.
	 * 
	 * @param text
	 *        the anchor's text
	 * @param asHTML
	 *        <code>true</code> to treat the specified text as html
	 * @param href
	 *        the url to which it will link
	 * @see Anchor
	 */
	public ImageAnchor(String text, boolean asHTML, String href) {
		super(text, asHTML, href);
	}
	
	/**
	 * Creates an anchor for scripting.
	 * 
	 * The anchor's href is set to <code>javascript:;</code>, based on the
	 * expectation that listeners will be added to the anchor.
	 * 
	 * @param text
	 *        the anchor's text
	 * @param asHtml
	 *        <code>true</code> to treat the specified text as html
	 * @see Anchor
	 */
	public ImageAnchor(String text, boolean asHtml) {
		super(text, asHtml);
	}
	
	/**
	 * Creates a source anchor with a frame target.
	 * 
	 * @param text
	 *        the anchor's text
	 * @param href
	 *        the url to which it will link
	 * @param target
	 *        the target frame (e.g. "_blank" to open the link in a new
	 *        window)
	 * @see Anchor
	 */
	public ImageAnchor(String text, String href, String target) {
		super(text, href, target);
	}
	
	/**
	 * Creates an anchor for scripting.
	 * 
	 * The anchor's href is set to <code>javascript:;</code>, based on the
	 * expectation that listeners will be added to the anchor.
	 * 
	 * @param text
	 *        the anchor's text
	 * @see Anchor
	 */
	public ImageAnchor(String text) {
		super(text);
	}
	
	/**
	 * Creates an anchor with its text and href (target URL) specified.
	 * 
	 * @param text
	 *        the anchor's text
	 * @param href
	 *        the url to which it will link
	 * @see Anchor
	 */
	public ImageAnchor(String text, String href) {
		super(text, href);
	}
	
	/**
	 * Creates an anchor with its {@link Image} and href (target URL) specified.
	 * 
	 * @param image
	 *        the anchor's image
	 * @param href
	 *        the url to which it will link
	 */
	public ImageAnchor(Image image, String href) {
		this("", href);
		setImage(image);
	}
	
	/**
	 * Creates an anchor with its {@link ImageResource} and href (target URL)
	 * specified.
	 * 
	 * @param image
	 *        the anchor's image
	 * @param href
	 *        the url to which it will link
	 */
	public ImageAnchor(ImageResource image, String href) {
		this("", href);
		setImage(image);
	}
	
	/**
	 * Creates a source anchor with an {@link Image} and a frame target.
	 * 
	 * @param image
	 *        the anchor's image
	 * @param href
	 *        the url to which it will link
	 * @param target
	 *        the target frame (e.g. "_blank" to open the link in a new
	 *        window)
	 */
	public ImageAnchor(Image image, String href, String target) {
		this("", href, target);
		setImage(image);
	}
	
	/**
	 * Creates a source anchor with an {@link ImageResource} and a frame target.
	 * 
	 * @param image
	 *        the anchor's image
	 * @param href
	 *        the url to which it will link
	 * @param target
	 *        the target frame (e.g. "_blank" to open the link in a new
	 *        window)
	 */
	public ImageAnchor(ImageResource image, String href, String target) {
		this("", href, target);
		setImage(image);
	}
	
	/**
	 * Sets the {@link ImageResource} for this anchor.
	 * All existing child elements will be removed.
	 * 
	 * @param image
	 */
	public void setImage(ImageResource image) {
		setImage(new Image(image));
	}
	
	/**
	 * Sets the {@link Image} for this anchor.
	 * All existing child elements will be removed.
	 * 
	 * @param image
	 */
	public void setImage(Image image) {
		UiUtils.setChildElement(this.getElement(), image.getElement());
	}
	
}
